/* eslint-disable no-eval */
"use strict";

const { Controller } = require("egg");

class CoreController extends Controller {
  async adapter() {
    const url = this.ctx.path;
    const headers = this.ctx.headers;
    const query = this.ctx.query;
    const body = this.ctx.body;
    const res = await this.app.model.TDynamicApi.findOne({
      where: {
        path: url,
      },
    });
    if (res) {
      if (res.type === "SQL") {
        const templateVariables = {
          body,
          headers,
          query,
          // sql,
        }
        const sql = await this.service.sql.executeQuery(res,templateVariables);
        templateVariables.sql = sql
        this.ctx.body = sql
        return 
        this.ctx.body = this.service.template.replaceJSONVariable(res.resBody, templateVariables);
      } else {
        this.ctx.body = res.body;
      }
    } else {
      this.ctx.body = {};
    }
  }
}

module.exports = CoreController;
